<template>
  <div>
    <el-row>
      <el-col :span="2">
        <el-button type="text" icon="el-icon-back">返回</el-button></el-col
      >
      <el-col
        :span="2"
        style="font-size: 20px; margin-top: 4px; font-weight: 800"
      >
        排班设置
      </el-col>
    </el-row>
    <div class="box">
      <el-button type="danger" @click="week">添加工作模式</el-button>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="工作模式编号"> </el-table-column>
        <el-table-column prop="name" label="工作模式"> </el-table-column>
        <el-table-column prop="workPatternTypeDesc" label="类型">
        </el-table-column>
        <el-table-column prop="workDate" label="工作日期"> </el-table-column>
        <el-table-column
          prop="workEndMinute1-workStartMinute1"
          label="工作时间"
        >
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-link type="warning" @click="edit(row.id)">编辑</el-link>
            <el-link type="danger" @click="delWeek(row.id)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @current-change="handleCurrentChange"
        :page-size="page.page"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      title="新增工作模式"
      :visible.sync="dialogVisible"
      width="50%"
      @close="close"
    >
      <el-tabs v-model="workType" @tab-click="handleClick">
        <el-tab-pane label="礼拜制" name="1">
          <!-- 礼拜制 -->
          <el-form label-position="left" label-width="120px">
            <el-form-item label="工作模式名称：">
              <el-input v-model="weekform.name"></el-input>
            </el-form-item>
            <el-form-item label="连续工作天数">
              <el-checkbox
                v-model="weekform.monday"
                label="周一"
                :true-label="1"
                :false-label="2"
              ></el-checkbox>
              <el-checkbox
                v-model="weekform.tuesday"
                label="周二"
                :true-label="1"
                :false-label="2"
              ></el-checkbox>
              <el-checkbox
                v-model="weekform.wednesday"
                label="周三"
                :true-label="1"
                :false-label="2"
              ></el-checkbox>
              <el-checkbox
                v-model="weekform.thursday"
                label="周四"
                :true-label="1"
                :false-label="2"
              ></el-checkbox>
              <el-checkbox
                v-model="weekform.friday"
                label="周五"
                :true-label="1"
                :false-label="2"
              ></el-checkbox>
              <el-checkbox
                v-model="weekform.saturday"
                label="周六"
                :true-label="1"
                :false-label="2"
              ></el-checkbox>
              <el-checkbox
                v-model="weekform.sunday"
                label="周日"
                :true-label="1"
                :false-label="2"
              ></el-checkbox>
            </el-form-item>
            <el-form-item label="工作时间">
              <el-time-picker
                is-range
                arrow-control
                v-model="value2"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                placeholder="选择时间范围"
                style="width: 100%"
              >
              </el-time-picker>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <!-- 连线制 -->
        <el-tab-pane label="连续制" name="2">
          <el-form label-position="left" label-width="120px">
            <el-form-item label="工作模式名称：">
              <el-input v-model="continuousform.name"></el-input>
            </el-form-item>
            <el-form-item label="连续工作天数">
              <span>上</span
              ><el-input
                style="width: 80px"
                v-model="continuousform.workDayNum"
              ></el-input>
              <span>休</span
              ><el-input
                style="width: 80px"
                v-model="continuousform.restDayNum"
              ></el-input>
            </el-form-item>
            <el-form-item label="工作时间">
              <el-time-picker
                is-range
                arrow-control
                v-model="value2"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                placeholder="选择时间范围"
                style="width: 100%"
              >
              </el-time-picker>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addWork">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getWorkPageApi,
  postWorkPageApi,
  delWorkPageApi,
  getWorkinfoApi,
  PUTWorkinfoApi,
} from "@/api/setting";
export default {
  data() {
    return {
      tableData: [{}],
      page: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      dialogVisible: false,
      workType: "1",
      weekform: {
        workPatternType: 1,
        name: "",
        monday: "2",
        tuesday: "2",
        wednesday: "2",
        thursday: "2",
        friday: "2",
        saturday: "2",
        sunday: "2",
        workStartMinute1: 0,
        workEndMinute1: 0,
      },
      continuousform: {
        workPatternType: 2,
        name: "",
        workDayNum: 0,
        restDayNum: 0,
        workStartMinute1: 0,
        workEndMinute1: 0,
      },
      checkList: [],
      value2: "",
    };
  },
  created() {
    this.getWorkPage();
  },
  methods: {
    async getWorkPage() {
      const res = await getWorkPageApi(this.page);
      this.total = +res.data.counts;
      this.tableData = res.data.items;
    },
    handleCurrentChange(num) {
      this.page.page = num;
      this.getWorkPage();
    },
    close() {
      this.dialogVisible = false;
      this.weekform = {
        workPatternType: 1,
        name: "",
        monday: "2",
        tuesday: "2",
        wednesday: "2",
        thursday: "2",
        friday: "2",
        saturday: "2",
        sunday: "2",
        workStartMinute1: 0,
        workEndMinute1: 0,
      };
      this.continuousform = {
        workPatternType: 2,
        name: "",
        workDayNum: 0,
        restDayNum: 0,
        workStartMinute1: 0,
        workEndMinute1: 0,
      };
    },
    handleClick() {},
    //添加工作模式
    week() {
      this.dialogVisible = true;
    },
    async addWork() {
      if (this.workType === "1") {
        if (this.weekform.id) {
          this.weekform.workEndMinute1 =
            (+this.value2[1] - +this.value2[0]) / 60000;
          await PUTWorkinfoApi(this.weekform);
          this.$message.success("编辑成功");
        } else {
          this.weekform.workEndMinute1 =
            (+this.value2[1] - +this.value2[0]) / 60000;
          await postWorkPageApi(this.weekform);
          this.$message.success("添加成功");
        }
      } else {
        if (this.continuousform.id) {
          this.continuousform.workEndMinute1 =
            (+this.value2[1] - +this.value2[0]) / 60000;
          await PUTWorkinfoApi(this.continuousform);
          this.$message.success("编辑成功");
        } else {
          this.continuousform.workEndMinute1 =
            (+this.value2[1] - +this.value2[0]) / 60000;
          await postWorkPageApi(this.continuousform);
          this.$message.success("添加成功");
        }
      }
      this.close();
      this.getWorkPage();
    },
    async delWeek(id) {
      await this.$confirm("确认删除？");
      await delWorkPageApi(id);
      this.$message.success("删除成功成功");
      this.getWorkPage();
    },
    async edit(id) {
      const res = await getWorkinfoApi(id);
      this.continuousform = res.data;
      this.workType = `${res.data.workPatternType}`;
      console.log(res.data);
      this.dialogVisible = true;
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  height: 700px;
  background-color: #fff;
  padding: 20px;
}
</style>